package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SmsUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *    移动端 套餐列表查询
 */
@RestController
@RequestMapping("order")
@Api(tags = "移动端-订单管理")
public class OrderController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Reference
    private OrderService orderService;

    @GetMapping("sendCode/{telephone}")
    @ApiOperation(value = "短信发送",notes = "用户预约，验证码发送说明")
    public Result sendCode(@PathVariable("telephone") String  telephone)
    {
         String code = ValidateCodeUtils.generateValidateCode(4);
        // SmsUtils.validUserTelephone(telephone,code);
        Map map = new HashMap();
        map.put("telephone",telephone);
        map.put("code",code);
        //rabbitTemplate.convertAndSend("heima-queue",map);
        System.out.println("----验证码："+code);
        RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER+telephone,code,90, TimeUnit.SECONDS);
        return  new Result(code);
    }

    @PostMapping("add")
    @ApiOperation(value = "预约",notes = "用户预约数据")
    public Result add(@RequestBody Map map)
    {
        return  new Result(orderService.add(map));
    }


    @GetMapping("findOrderInfoByOrderId/{id}")
    @ApiOperation(value = "预约成功",notes = "根据订单id 查询订单详情")
    public Result findOrderInfoByOrderId(@PathVariable("id") int id)
    {
        return  new Result(orderService.findOrderInfoByOrderId(id));
    }


}
